package scc.project.web.controller.manage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import scc.project.entity.*;
import scc.project.entity.pojo.SellInfoPojo;
import scc.project.entity.vo.JsonVo;
import scc.project.entity.vo.PageVo;
import scc.project.entity.vo.RestFulPageVo;
import scc.project.entity.vo.SellInfoVo;
import scc.project.service.CustomService;
import scc.project.service.GoodsService;
import scc.project.service.SellInfoService;
import scc.project.util.HttpUtil;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 销售管理的控制器
 *
 * @author 陈圳佳
 *         日期：2015/6/22
 */
@Controller
@RequestMapping("/manage/sales")
public class SalesController {

    @Autowired
    private CustomService customService;
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private SellInfoService sellInfoService;

    @RequestMapping(value = "/edit.html", method = RequestMethod.GET)
    public String edit(ModelMap modelMap) {
        try {
            List<Custom> customs = customService.getAll();
            modelMap.addAttribute("customs", customs);
            List<Goods> goodsList = goodsService.getAll();
            modelMap.addAttribute("goodsList", goodsList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/manage/sales/edit";
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JsonVo add(@RequestBody SellInfoVo sellInfo, HttpSession session) {
        JsonVo jsonVo = new JsonVo();
        Employee emp = HttpUtil.getEmp(session);
        sellInfo.setEmpId(emp.getId());
        try {
            boolean b = sellInfoService.save(sellInfo);
            jsonVo.setStatus(b).setMsg(b ? "添加成功" : "添加失败了!原因未知");
        } catch (Exception e) {
            jsonVo.setMsg(e.getLocalizedMessage()).setStatus(false);
            e.printStackTrace();
        }
        return jsonVo;
    }

    @RequestMapping(value = "/list/{page}.html", method = RequestMethod.GET)
    public String list(@PathVariable("page") Integer page,
                       @RequestParam(value = "emp", required = false) String emp,
                       @RequestParam(value = "custom", required = false) String custom,
                       ModelMap modelMap) {
        try {
            PageVo<SellInfo> infoVoPageVo = sellInfoService.find(page, emp, custom);
            RestFulPageVo<SellInfo> pageVo = RestFulPageVo.convert(infoVoPageVo, ".html");
            modelMap.addAttribute("pageVo", pageVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/manage/sales/list";
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public SellInfoPojo get(@PathVariable("id") Long id) {
        try {
            return sellInfoService.get(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
